﻿/* 
Copyright (c) 2009, Aaron Moline <Aaron.Moline@MolineSoftware.com>

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, 
publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
*/
//using System;
//using CookComputing.XmlRpc;
//using System.Collections.Generic;
//using System.Reflection;
//using System.Collections;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Reflection;
using MolineSoftware.DrupalXmlRpc.Interfaces;
using MolineSoftware.DrupalXmlRpc.Classes;
using CookComputing.XmlRpc;

namespace MolineSoftware.DrupalXmlRpc
{
    /// <summary>
    /// Class : BaseClass for all DrupalNode objects.
    /// </summary>
    [XmlRpcMissingMapping(MappingAction.Ignore)]
    public class DrupalBase
    {

    }

    /// <summary>
    /// Struct: Drupal Node. 
    /// </summary>    
    public class DrupalNode : DrupalBase
    {
        #region | Constructors |
        /// <summary>
        /// Instatiate a new instance of <see cref="DrupalNode"/>
        /// </summary>
        public DrupalNode()
        {
            this.Intialize();
        }
        #endregion

        #region | Public Static Methods |
        /// <summary>
        /// Intialize all <see cref="XmlRpcStruct"/> objects that are in class.
        /// </summary>
        public void Intialize()
        {
            foreach (PropertyInfo pi in this.GetType().GetProperties())
            {
                if (pi.PropertyType == typeof(XmlRpcStruct[]))
                {
                    pi.SetValue(this, new XmlRpcStruct[1], new object[] { });

                }
            }
        }

        /// <summary>
        /// Set Value in <see cref="DrupalNode"/>
        /// </summary>
        /// <param name="node"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="rpcLable"></param>
        public void SetValue(string propertyName, object value, string rpcLable)
        {
            PropertyInfo pi = this.GetType().GetProperties().Where(p => p.Name.ToUpper() == propertyName.ToUpper()).Single();
            if (pi.PropertyType == typeof(XmlRpcStruct[]))
            {
                XmlRpcStruct[] xList = new XmlRpcStruct[1];
                xList[0] = new XmlRpcStruct();
                xList[0].Add(rpcLable, value);
                pi.SetValue(this, xList, null);
            }
            else if (pi.PropertyType == typeof(XmlRpcStruct))
            {
                XmlRpcStruct x = new XmlRpcStruct();
                x.Add(rpcLable, value);
                pi.SetValue(this, x, null);
            }
            else
                pi.SetValue(this, value, null);
        }

        /// <summary>
        /// Gets the Core Fields for returning a Node.
        /// </summary>
        public static string[] BaseFields
        {
            get
            {
                return new string[] { "body", "title", "nid" };
            }
        }

        /// <summary>
        /// Gets All Availble Fields
        /// </summary>
        public static string[] AllFields
        {
            get { return new string[] { }; }
        }
        #endregion

        #region Public Properites
        /// <summary>
        /// Gets or Sets the NodeID
        /// </summary>
        [XmlRpcMember(Description = "The NodeID", Member = "nid")]
        public string NodeID { get; set; }

        /// <summary>
        /// Gets or Sets the type
        /// </summary>
        [XmlRpcMember(Description = "", Member = "type")]
        public string Type { get; set; }

        /// <summary>
        /// Gets or Sets the Language
        /// </summary>
        [XmlRpcMember(Description = "", Member = "language")]
        public string Language { get; set; }

        /// <summary>
        /// Gets or Sets the UserID
        /// </summary>
        [XmlRpcMember(Description = "", Member = "uid")]
        public string UserID { get; set; }

        /// <summary>
        /// Gets or Sets the Publish
        /// </summary>
        [XmlRpcMember(Description = "", Member = "status")]
        public string Publish { get; set; }

        /// <summary>
        /// Gets or Sets the CreatenDate
        /// </summary>
        [XmlRpcMember(Description = "", Member = "created")]
        public string CreatenDate { get; set; }

        /// <summary>
        /// Gets or Sets the ChangeDate
        /// </summary>
        [XmlRpcMember(Description = "", Member = "changed")]
        public string ChangeDate { get; set; }

        /// <summary>
        /// Gets or Sets the Comment
        /// </summary>
        [XmlRpcMember(Description = "", Member = "comment")]
        public string Comment { get; set; }

        /// <summary>
        /// Gets or Sets the Moderate
        /// </summary>
        [XmlRpcMember(Description = "", Member = "moderate")]
        public string Moderate { get; set; }

        /// <summary>
        /// Gets or Sets the Sticky
        /// </summary>
        [XmlRpcMember(Description = "", Member = "sticky")]
        public string Sticky { get; set; }

        /// <summary>
        /// Gets or Sets the TNodeID
        /// </summary>
        [XmlRpcMember(Description = "", Member = "tnid")]
        public string TNodeID { get; set; }

        /// <summary>
        /// Gets or Sets the Translate
        /// </summary>
        [XmlRpcMember(Description = "", Member = "translate")]
        public string Translate { get; set; }

        /// <summary>
        /// Gets or Sets the VID
        /// </summary>
        [XmlRpcMember(Description = "", Member = "vid")]
        public string VID { get; set; }

        /// <summary>
        /// Gets or Sets the RevisionUID
        /// </summary>
        [XmlRpcMember(Description = "", Member = "revision_uid")]
        public string RevisionUID { get; set; }

        /// <summary>
        /// Gets or Sets the Title
        /// </summary>
        [XmlRpcMember(Description = "", Member = "title")]
        public string Title { get; set; }

        /// <summary>
        /// Gets or Sets the Body
        /// </summary>
        [XmlRpcMember(Description = "", Member = "body")]
        public string Body { get; set; }

        /// <summary>
        /// Gets or Sets the Teaser
        /// </summary>
        [XmlRpcMember(Description = "", Member = "teaser")]
        public string Teaser { get; set; }

        /// <summary>
        /// Gets or Sets the Log
        /// </summary>
        [XmlRpcMember(Description = "", Member = "log")]
        public string Log { get; set; }

        /// <summary>
        /// Gets or Sets the RevisionDate
        /// </summary>
        [XmlRpcMember(Description = "The last time the node was revised", Member = "revision_timestamp")]
        public string RevisionDate { get; set; }

        /// <summary>
        /// Gets or Sets the Format
        /// </summary>
        [XmlRpcMember(Description = "", Member = "format")]
        public string Format { get; set; }

        /// <summary>
        /// Gets or Sets the UserName
        /// </summary>
        [XmlRpcMember(Description = "Who created the node.", Member = "name")]
        public string UserName { get; set; }

        /// <summary>
        /// Gets or Sets the Picture
        /// </summary>
        [XmlRpcMember(Description = "", Member = "picture")]
        public string Picture { get; set; }

        /// <summary>
        /// Gets or Sets the Data
        /// </summary>
        [XmlRpcMember(Description = "", Member = "data")]
        public string Data { get; set; }

        /// <summary>
        /// Gets or Sets the DrupalBook
        /// </summary>
        [XmlRpcMember(Description = "", Member = "book")]
        public XmlRpcStruct DrupalBook { get; set; }

        /// <summary>
        /// Gets or Sets the LastCommentDate
        /// </summary>
        [XmlRpcMember(Description = "", Member = "last_comment_timestamp")]
        public string LastCommentDate { get; set; }

        /// <summary>
        /// Gets or Sets the CommentCount
        /// </summary>
        [XmlRpcMember(Description = "", Member = "last_comment_name")]
        public string LastCommentName { get; set; }

        /// <summary>
        /// Gets or Sets the CommentCount
        /// </summary>
        [XmlRpcMember(Description = "", Member = "comment_count")]
        public string CommentCount { get; set; }

        /// <summary>
        /// Gets or Sets the Taxonomy
        /// </summary>
        [XmlRpcMember(Description = "", Member = "taxonomy")]
        public string[] Taxonomy { get; set; }

        /// <summary>
        /// Gets or Sets the Files
        /// </summary>
        [XmlRpcMember(Description = "", Member = "files")]
        public string[] Files { get; set; }

        /// <summary>
        /// Gets or Sets the BodyValue
        /// </summary>
        [XmlRpcMember(Description = "", Member = "body_value")]
        public string BodyValue { get; set; }

        /// <summary>
        /// Gets or Sets the Promote
        /// </summary>
        [XmlRpcMember(Description = "", Member = "promote")]
        public string Promote { get; set; }
        #endregion         
    }

    /*
stdClass Object
(
    [nid] => 2
    [type] => book
    [language] => en
    [uid] => 1
    [status] => 1
    [created] => 1235762868
    [changed] => 1235786200
    [comment] => 2
    [promote] => 0
    [moderate] => 0
    [sticky] => 0
    [tnid] => 0
    [translate] => 0
    [vid] => 2
    [revision_uid] => 3
    [title] => child page one
    [body] => <p>orem ipsum dolor sit amet, consectetuer adipiscing elit. Donec tempus. Curabitur vel libero eget elit mollis mattis. Sed ut risus. Donec urna. Etiam tortor mauris, porttitor non, viverra et, bibendum bibendum, felis. Duis sed mauris at justo eleifend porttitor. In aliquam. In sit amet nisl eget neque fringilla eleifend. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. In hac habitasse platea dictumst. Ut sed diam. Aenean tristique nisi a turpis. Maecenas viverra, velit non mollis ornare, enim nibh consectetuer elit, nec aliquet est diam a lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi nulla. Donec bibendum venenatis enim. Phasellus egestas feugiat nibh. Mauris enim. Aliquam lectus.<br />
Sed ante. Suspendisse felis urna, rhoncus vitae, tempus vitae, elementum nec, sapien. Donec eget orci. Ut mauris turpis, scelerisque pharetra, ornare ut, laoreet in, ligula. Donec at erat laoreet dui laoreet posuere. Vivamus nibh. Nunc elementum rutrum lorem. Sed luctus nisl non arcu. Curabitur enim neque, consectetuer sed, volutpat nec, commodo sed, nibh. Nullam dictum porttitor dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla dolor mauris, dapibus id, fringilla vitae, sodales ut, augue. Morbi eget libero et odio imperdiet dapibus. Phasellus pharetra, est a fringilla faucibus, diam turpis cursus metus, eget ullamcorper nulla arcu et justo. Phasellus egestas.</p>

    [teaser] => <p>orem ipsum dolor sit amet, consectetuer adipiscing elit. Donec tempus. Curabitur vel libero eget elit mollis mattis. Sed ut risus. Donec urna. Etiam tortor mauris, porttitor non, viverra et, bibendum bibendum, felis. Duis sed mauris at justo eleifend porttitor. In aliquam. In sit amet nisl eget neque fringilla eleifend. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. In hac habitasse platea dictumst. Ut sed diam. Aenean tristique nisi a turpis. Maecenas viverra, velit non mollis ornare, enim nibh consectetuer elit, nec aliquet est diam a lectus.
    [log] => 
    [revision_timestamp] => 1235786200
    [format] => 1
    [name] => Administrator
    [picture] => 
    [data] => a:0:{}
    [book] => Array
        (
            [mlid] => 213
            [nid] => 2
            [bid] => 1
            [menu_name] => book-toc-1
            [plid] => 212
            [link_path] => node/2
            [router_path] => node/%
            [link_title] => child page one
            [options] => Array
                (
                )

            [module] => book
            [hidden] => 0
            [external] => 0
            [has_children] => 1
            [expanded] => 0
            [weight] => 0
            [depth] => 2
            [customized] => 0
            [p1] => 212
            [p2] => 213
            [p3] => 0
            [p4] => 0
            [p5] => 0
            [p6] => 0
            [p7] => 0
            [p8] => 0
            [p9] => 0
            [updated] => 0
            [href] => node/2
            [title] => child page one
        )

    [last_comment_timestamp] => 1235762868
    [last_comment_name] => 
    [comment_count] => 0
    [taxonomy] => Array
        (
        )

    [files] => Array
        (
        )

    [body_value] => <p>orem ipsum dolor sit amet, consectetuer adipiscing elit. Donec tempus. Curabitur vel libero eget elit mollis mattis. Sed ut risus. Donec urna. Etiam tortor mauris, porttitor non, viverra et, bibendum bibendum, felis. Duis sed mauris at justo eleifend porttitor. In aliquam. In sit amet nisl eget neque fringilla eleifend. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. In hac habitasse platea dictumst. Ut sed diam. Aenean tristique nisi a turpis. Maecenas viverra, velit non mollis ornare, enim nibh consectetuer elit, nec aliquet est diam a lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi nulla. Donec bibendum venenatis enim. Phasellus egestas feugiat nibh. Mauris enim. Aliquam lectus.<br />
Sed ante. Suspendisse felis urna, rhoncus vitae, tempus vitae, elementum nec, sapien. Donec eget orci. Ut mauris turpis, scelerisque pharetra, ornare ut, laoreet in, ligula. Donec at erat laoreet dui laoreet posuere. Vivamus nibh. Nunc elementum rutrum lorem. Sed luctus nisl non arcu. Curabitur enim neque, consectetuer sed, volutpat nec, commodo sed, nibh. Nullam dictum porttitor dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla dolor mauris, dapibus id, fringilla vitae, sodales ut, augue. Morbi eget libero et odio imperdiet dapibus. Phasellus pharetra, est a fringilla faucibus, diam turpis cursus metus, eget ullamcorper nulla arcu et justo. Phasellus egestas.</p>


     */
}
